package entity_model.json.serializers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import entity_model.Entity;

import java.lang.reflect.Type;

/**
* Created by nuno-rafael-figueiredo on 13-05-2014.
*/
public class EntitySerializer implements JsonSerializer<Entity> {

    @Override
    public JsonElement serialize(Entity src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name", src.getName());

        if (src.getBaseEntity() != null)
            jsonObject.addProperty("base", src.getBaseEntity().getName());

        if (src.isAbstract())
            jsonObject.addProperty("abstract", true);

        if (src.getAttributes().size() > 0)
            jsonObject.add("attributes", context.serialize(src.getAttributes()));

        return jsonObject;
    }
}
